package com.ophiux.cs.infrastructure.repositories.user;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ophiux.cs.domain.user.model.aggregate.UserPatientInfo;
import com.ophiux.cs.domain.user.model.entity.Patients;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.model.valueobj.PatientsValueObj;
import com.ophiux.cs.domain.user.repositories.IPatientRepository;
import com.ophiux.cs.infrastructure.mapper.user.PatientMapper;
import com.ophiux.cs.infrastructure.mapper.user.UserMapper;
import com.ophiux.utils.common.StringUtil;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Repository
@Slf4j
public class PatientRepository implements IPatientRepository {
	@Autowired
	PatientMapper patientMapper;
	@Autowired
	UserMapper userMapper;
	
	@Override
	public Patients getPatientsByPatientId(String patientId) throws OphiuxRepositoryException {
		Patients info = null;
		try {
			info = patientMapper.getPatientsByPatientId(patientId);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
		return info;
	}
	
	@Override
	public UserPatientInfo getPatientsInfoList(PatientsValueObj obj) throws OphiuxRepositoryException {
		UserPatientInfo userPatientInfo = new UserPatientInfo();
		try {
			User user = userMapper.getUserByUserId(obj.getUSER_ID());
			if(user==null) {
				return null;
			}
			userPatientInfo.setUSER_ID(obj.getUSER_ID());
			
			//查询该用户下面的所有就诊人
			obj.setPATIENT_ID("");
			List<Patients> list = patientMapper.getPatientsInfoList(obj);
			if(list!=null) {
				userPatientInfo.setPatients(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
		return userPatientInfo;
	}

	@Override
	public void save(Patients patients) throws OphiuxRepositoryException {
		try {
			Patients newPatient = patientMapper.getPatientsByPatientId(patients.getPATIENT_ID());
			
			if(newPatient==null) {//数据库没有,则执行新增操作
				patientMapper.insert(patients);
			}else {
				patientMapper.update(patients);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
	}
	

	@Override
	public void deletePatients(Patients patients) throws OphiuxRepositoryException{
		try {
			patientMapper.update(patients);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	@Transactional
	public void save(UserPatientInfo userPatientInfo) throws OphiuxRepositoryException {
		try {
			List<Patients> patientsList = new ArrayList<Patients>();
			patientsList = userPatientInfo.getPatients();
			for (Patients patients : patientsList) {
				if(StringUtil.isEmpty(patients.getID())){
					patientMapper.insert(patients);
				}else {
					patientMapper.update(patients);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
	}

	
}
